#ifndef SOCKETPOOL_H_
#define SOCKETPOOL_H_

#include "Common.h"
#include "ConfManager.h"

class ServerSocket
{
    public:
        ServerSocket():fd(-1),avail(false){}
        int32 fd;
        bool avail;
        timeval_t failedTime;
};

class GroupSocket
{
    public:
        GroupSocket():sCnt(0),serverSocket(NULL){}

        int32 sCnt;
        ServerSocket *serverSocket;
};

class ThreadSocket
{
    public:
        ThreadSocket():gCnt(0),groupSocket(NULL){}

        int32 gCnt;
        GroupSocket *groupSocket;
};

class SocketPool
{
    private:

        SocketPool(Terminal terminal, int32 threadCnt, int32 retryTime);

        static SocketPool *_instance;

        static pthread_mutex_t _mutex_init;

        Terminal _terminal;

        int32 _threadCnt;

        int32 _retryTime;

        ThreadSocket *_threadSocket;

    public:

        ~SocketPool();

        static SocketPool *Instance();

        void Start();

        void Stop();

        int32 GetGroupCnt();

        int32 GetServerCnt(int32 gNum);

        bool GetIPPort(int32 gNum, int32 sNum, std::string &ip, int32 &port);

        int32 GetSocketFd(std::string IP, int32 port);

        bool GetSocket(int32 threadNum, int32 groupNum, int32 &fd, int32 &sNo);

        void ResetSocket(int32 threadNum, int32 groupNum, int32 sNo);
};

#endif
